//
//  MyPowerController.m
//  TrainTree
//
//  Created by wanghui on 2017/9/11.
//  Copyright © 2017年 YouYeShu. All rights reserved.
//

#import "MyPowerController.h"
#import "MyPowerTopCell.h"
#import "MyPowerDetailCell.h"
#import "MyPowerRecordCell.h"
#import "MineRequestManager.h"
@interface MyPowerController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)NSArray *listArray;
@property (strong,nonatomic)NSDictionary *powerDetail;
@property (assign,nonatomic)NSNumber *powerCount;
@end

@implementation MyPowerController

- (NSArray *)listArray
{
    return _listArray ?: (_listArray = [NSArray array]);
}
- (void)viewDidLoad {
    [super viewDidLoad];
        //    self.navigationView.title = @"我的能量";
    self.navigationView.backgroundColor = [UIColor clearColor];
    self.navigationView.titleIcon.hidden = YES;
    [self.navigationView.leftBtn setImage:ImageNamed(@"back") forState:UIControlStateNormal];
    [self.navigationView.leftBtn addTarget:self action:@selector(backToLastPage) forControlEvents:UIControlEventTouchUpInside];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"MyPowerDetailCell" bundle:nil] forCellReuseIdentifier:@"MyPowerDetailCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyPowerTopCell" bundle:nil] forCellReuseIdentifier:@"MyPowerTopCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyPowerRecordCell" bundle:nil] forCellReuseIdentifier:@"MyPowerRecordCell"];
    [self requestPowerData];
}
#pragma mark - 获取能量泉数据
- (void)requestPowerData
{
    WeakSelf(ws);
    [MineRequestManager requestPowerDataResponse:^(id JSONData, NSError *error, NSURLSessionDataTask *dataTask) {
        if (JSONData) {
            ws.powerCount = JSONData[@"d"][@"powerCount"];
            ws.listArray = JSONData[@"d"][@"recordList"];
            ws.powerDetail = JSONData[@"d"][@"powerDetail"];
            [ws.tableView reloadData];
        }
    }];
}
- (void)backToLastPage
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2 + self.listArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 212.0f / 375.0f * ScreenWidth;
    } else if (indexPath.row == 1) {
        return 125.0f;
    } else {
        return 60.0f;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        MyPowerTopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyPowerTopCell"];
        [cell.powerNumber setTitle:[NSString stringWithFormat:@"%@", self.powerCount] forState:UIControlStateNormal];
        return cell;
    } else if (indexPath.row == 1) {
        MyPowerDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyPowerDetailCell"];
        if (self.powerDetail) {
            cell.watchVideo.text = [NSString stringWithFormat:@"%@",self.powerDetail[@"videoCount"]];
            cell.finishLesson.text = [NSString stringWithFormat:@"%@", self.powerDetail[@"lessonCount"]];
            cell.submitNumber.text = [NSString stringWithFormat:@"%@",self.powerDetail[@"feedbackCount"]];
        }
        return  cell;
    } else {
        MyPowerRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyPowerRecordCell"];
        if (self.listArray.count > 0) {
            cell.reason.text = self.listArray[indexPath.row - 2][@"mode"];
            cell.time.text = self.listArray[indexPath.row - 2][@"date"];
            cell.count.text = [NSString stringWithFormat:@"+ %@", self.listArray[indexPath.row - 2][@"power"]];
        }
        return cell;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

